<?php

class AdministratorForm extends Zend_Form
{
    private $picture_path = "";

    public function __construct($options=array())
    {
        parent::__construct($options);

        // set form name and POST
        $this->setName('user_account');
        $this->setMethod('post');
        $this->setEnctype('multipart/form-data');
		$this->setAction($options['action']);
		
		$show_auth_fields = $options['show_auth_fields'];
		
        // create basic field decorators
        $basicDecorators = array(
            'ViewHelper',
            array('Errors', array('placement'=>'prepend')),
            //array(array('labelBr'=>'HtmlTag'), array('tag'=>'br')),
            array('Label', array('placement'=>'prepend')),
            array( array('outerDiv'=>'HtmlTag'), array('tag'=>'div', 'style'=>'margin-top:5px')),
        );

		$f_fname = new Zend_Form_Element_Text( 'fname', 
		array('size'=>30, 'label'=>'First Name', 'required'=>true));
		$f_fname->setDecorators($basicDecorators);
		$f_mname = new Zend_Form_Element_Text( 'mname', 
		array('size'=>30, 'label'=>'Middle Name','required'=>false));
		$f_mname->setDecorators($basicDecorators);
		$f_lname = new Zend_Form_Element_Text( 'lname', 
		array('size'=>40, 'label'=>'Last Name','required'=>true));
		$f_lname->setDecorators($basicDecorators);
		
		$f_address = new Zend_Form_Element_Text( 'address', array('size'=>48, 'label'=>'Address','required'=>true));
		$f_address->setDecorators($basicDecorators);
		$f_city = new Zend_Form_Element_Text( 'city', array('size'=>40, 'label'=>'City','required'=>true));
		$f_city->setDecorators($basicDecorators);
		$f_state = new Zend_Form_Element_Text( 'state', array('size'=>2, 'label'=>'State','required'=>true));
		$f_state->setDecorators($basicDecorators);
		$f_zip = new Zend_Form_Element_Text( 'zip', array('size'=>10, 'label'=>'Zip','required'=>true));
		$f_zip->setDecorators($basicDecorators);
		
		$f_phone = new Zend_Form_Element_Text( 'phone', array('size'=>21, 'label'=>'Phone','required'=>true));
		$f_phone->setDecorators($basicDecorators);
		$f_email = new Zend_Form_Element_Text( 'email', array('size'=>60, 'label'=>'Email','required'=>true));
		$f_email->setDecorators($basicDecorators);
		
		if($show_auth_fields)
		{
	        $username = new Zend_Form_Element_Text('username', array('size'=>20, 'label'=>'Username', 'required'=>true));
			$username->setDecorators($basicDecorators);
	        $password = new Zend_Form_Element_Text( 'password', array('size'=>20, 'label'=>'Password', 'required'=>true));
			$password->setDecorators($basicDecorators);
		}
		
        // form's submit button
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Submit');

		$this->addElements( array( $f_fname, $f_mname, $f_lname ) );
		$this->addElements( array( $f_address, $f_city, $f_state, $f_zip ) );		
		$this->addElements( array( $f_phone, $f_email ) );
		if($show_auth_fields)
	        $this->addElements( array( $username, $password ) );
		$this->addElements( array( $submit ) );
    }

    // dirt-simple template engine.
    public function render_with_template( $template_name )
    {
        $template_file_path = APP_DIR."/views/templates/$template_name";
        $template_contents = file_get_contents( $template_file_path );
		$formAction = $this->getAction();
		
        // str_replace() keywords, adding errors where needed
        $form_elements = $this->getElements();
		$template_contents = str_replace( "{ACTION}", $formAction, $template_contents);

		$str = $form_elements['fname'];		
        $template_contents = str_replace( "{FNAME}", $str, $template_contents );
		
        $template_contents = str_replace( "{MNAME}", $form_elements['mname'], $template_contents );

        $template_contents = str_replace( "{LNAME}", $form_elements['lname'], $template_contents );

        $template_contents = str_replace( "{ADDRESS}", $form_elements['address'], $template_contents );

        $template_contents = str_replace( "{CITY}", $form_elements['city'], $template_contents );

        $template_contents = str_replace( "{STATE}", $form_elements['state'], $template_contents );

        $template_contents = str_replace( "{ZIP}", $form_elements['zip'], $template_contents );		

        $template_contents = str_replace( "{PHONE}", $form_elements['phone'], $template_contents );

        $template_contents = str_replace( "{EMAIL}", $form_elements['email'], $template_contents );		

        $template_contents = str_replace( "{USERNAME}", $form_elements['username'], $template_contents );

        $template_contents = str_replace( "{PASSWORD}", $form_elements['password'], $template_contents );

        $template_contents = str_replace( "{SUBMIT_BUTTON}", $form_elements['submit'], $template_contents );

        echo $template_contents;
    }
}

?>
